/*************************************************************************
	> File Name: P1165.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-02-10 11:37:57
 ************************************************************************/
#include <iostream>
#include <stack>
using namespace std;

int main() {
    stack<int> s, ms;
    int n;
    scanf("%d", &n);
    while (n--) {
        int opt, x;
        scanf("%d", &opt);
        switch (opt) {
            case 0: {
                scanf("%d", &x);
                s.push(x);
                if (ms.empty() || x > ms.top()) ms.push(x);
                else ms.push(ms.top());
            } break;
            case 1: {
                if (!s.empty()) {
                    s.pop();
                    ms.pop();
                }
            } break;
            case 2: {
                if (ms.empty()) printf("0\n");
                else printf("%d\n", ms.top());
            } break;
        }
    }
    return 0;
}
